padVector <- function(vector,desiredLength){
  if(length(vector) < desiredLength){
    paddingLength = desiredLength - length(vector)
    return(append(vector,rep(vector[length(vector)],paddingLength)))
  }
  return(vector)
}

extractData <- function(inputFile,columnNumber){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  #return(list(inputData[,3],inputData[,5],inputData[,1]))
  return(inputData[,as.numeric(columnNumber)])
}

addErrorBars <- function(x,yplus,yminus,intersamples,color,lineType){
  samples <- seq(from=0,to=length(x),by=intersamples)
  arrows(x[samples],yplus[samples], x[samples], yminus[samples],code=3,angle=90,length="0.1",col=color,lty=lineType)
}

#############################################################################
library(extrafont)

# $1 - Directory where the .occ files corresponding to the different experiments are stored
# $2 - Experiment duration (seconds)
args <- commandArgs(trailingOnly = TRUE)
workingDirectory <- args[1]
colNumber <- args[2]
rm(args)

#Set the directory containing the experiment results as current working directory
setwd(paste("/home/deste/argos2/user/jdestefani/results/Comparisons/",workingDirectory,"/",sep=""))
#List all the files containing information about occupation
resultsFiles <- list.files(pattern = "\\.cmp$")
colString <- ""
print(resultsFiles)

#if(colNumber == "3"){
  #colString <- "Median"
#}

if(colNumber == "3"){
  colString <- "Median"
}

if(colNumber == "6"){
  colString <- "Mean"
}

print("[STATUS] - Processing results file")
#Process every single file separately
printData <- lapply(resultsFiles,extractData,columnNumber=colNumber)
resultsFiles <-  strsplit(resultsFiles,"\\.")
resultsFiles <- lapply(resultsFiles,function(x) return(x[2]))

print("[STATUS] - Producing plots")
#lineTypes <- c(1,2,4,5,6,1)
#colors <- rainbow(length(printData))
lineTypes <- c(1,2,4,5,6)
colors <- c("black","gray25","#606060","gray50","gray75","#DFDFDF")
pdf(paste(workingDirectory,colString,"pdf",sep="."),family="Droid Sans")
plot(range(0,10000),range(0,8),type="n",xlab="Time steps",ylab="Robots",main=paste("Max Error -",colString))
#Plot lines
lapply(seq(length(printData)),FUN=function(x) lines(printData[[x]],col=colors[x],lty=lineTypes[x],pch="."))

#Plot error bars
#lapply(seq(length(printData)),FUN=function(x) addErrorBars(x=seq(from=0,to=length(printData[[x]][[1]])),yplus=printData[[x]][[2]],yminus=printData[[x]][[3]],intersample=300,color=colors[x],lineType=lineTypes[x]))
#legend(x="bottomright", c("Probabilistic","Informed","Naive") , cex=1, col=colors, lty=lineTypes, title="Method")

#Plot area between curves, useful to represent quantiles
#print(length(c(seq(0,10000),seq(10000,0))))
#print(length(c(printData[[1]],rev(printData[[2]]))))
#polygon(c(seq(0,9999),seq(9999,0)),c(printData[[1]],rev(printData[[2]])),col="skyblue",border=NA)

legend(x="bottomright", legend=resultsFiles, horiz=TRUE, cex=0.6, col=colors, lty=lineTypes, title=("Method"))
dev.off()

rm(resultsFiles)
rm(printData)
#createTexGraph("t","$\\max_i(e_i(t))$",5,5,inputData[,1],maxErrorIntegrated[,1])
